//
//  TKIMToolBarBottomBaseView.m
//  TKIM
//  工具栏下面的工具基视图
//  Created by liupm on 15/10/15.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMToolBarBottomBaseView.h"

#define PlistName @"TKIMToolBar"

//#define offset 63

@interface TKIMToolBarBottomBaseView()<UIScrollViewDelegate>


@end


@implementation TKIMToolBarBottomBaseView
{

    UIPageControl *_pageControl;
    NSInteger dataoffset;

}

/**
 *  @Author lpm, 15-08-03 14:08:18
 *
 *  @brief 初始化数据
 */
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _row = 3;
        _cloum = 7;
        dataoffset=0;
        _isShowPageCount = YES;
    }
    return self;
}

/**
 *  @author zhengrs, 16-01-25 14:01:16
 *
 *  @brief 是否显示分页控件
 *
 *  @param isShowPageCount
 */
-(void)setIsShowPageCount:(BOOL)isShowPageCount
{
    if (isShowPageCount==NO) {
        [_pageControl removeFromSuperview];
    }
}

/**
 *  @Author lpm, 15-08-03 14:08:18
 *
 *  @brief 初始化表情数据
 */
-(void)initData
{
    //交给子类重写
    [self drawView:[self getConfigDataByKey:@""]];
}



/**
 *  @Author lpm, 15-08-03 16:08:11
 *
 *  @brief  组装界面
 */
-(void)drawView:(NSMutableArray *)_dataArray
{
    if (!_dataArray||_dataArray.count == 0)
    {
        return;
    }
    
    CGFloat width = self.frame.size.width/_cloum;
    CGFloat height = width;

    if (_cloum == 1)
    {
        height = self.frame.size.height-30;
    }
    
    if (_dataArray.count>60) {
        dataoffset = 63;
    }
    
    //页数
    NSInteger page = (_dataArray.count-dataoffset)/(_row*_cloum);
    
    //余数
    NSInteger y = (_dataArray.count-dataoffset)%(_row*_cloum);
    if (y!=0)
    {
        page += 1;
    }
    
    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    view.showsHorizontalScrollIndicator = NO;
    view.showsVerticalScrollIndicator = NO;
    view.contentSize = CGSizeMake(self.frame.size.width*page, self.frame.size.height);
    view.pagingEnabled = YES;
    view.delegate = self;
    
    //组装表情键盘
    for(int p = 0; p<page;p++)
    {
        NSInteger beginIndex = 3*_cloum*p+dataoffset;
        if (beginIndex > _dataArray.count-1)
        {
            break;
        }
        for (int i = 0; i<_row; i++)
        {
            CGFloat x = p * self.frame.size.width;
            CGFloat y = i*height;
            NSInteger index = beginIndex + i*_cloum ;
            
            if (index > _dataArray.count-1)
            {
                break;
            }
            for (int j = 0; j<_cloum; j++) {
                if (index > _dataArray.count-1)
                {
                    break;
                }
                
                NSMutableDictionary *dic = _dataArray[index];
                
                UIView *item = [self getItem:dic frame:CGRectMake(x, y, width, height)];
                item.tag = index;
                if (item) {
                    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doAction:)];
                    [item addGestureRecognizer:singleTap];
                    item.userInteractionEnabled = YES;
                    [view addSubview:item];
                }
                x += width;
                index++;
            }
        }
    }
    [self addSubview:view];
    
    if (_isShowPageCount)
    {
        _pageControl = [[UIPageControl alloc]  initWithFrame:CGRectMake(0, self.frame.size.height-35, self.frame.size.width, 30)];
        _pageControl.numberOfPages = page;
        _pageControl.currentPage = 0;
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
        [self addSubview:_pageControl];
    }
    
}

/**
 *  @Author lpm, 15-08-03 14:08:18
 *
 *  @brief 获得plist配置文件的数据
 */
-(NSMutableArray *)getConfigDataByKey:(NSString *)key
{
    NSMutableArray *dataArray = [NSMutableArray array];
    
    @try
    {
        
        NSString *plistUrl = TKIMSrcName(@"TKIMToolBar.plist");
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:plistUrl];
        if (dic)
        {
            NSDictionary *data = dic[key];
            if (data)
            {
                NSArray *keys = data[@"keys"];
                NSArray *values = data[@"values"];
                for (int i=0; i<keys.count; i++)
                {
                    NSMutableDictionary *obj = [NSMutableDictionary dictionary];
                    obj[@"key"] = keys[i];
                    obj[@"value"] = values[i];
                    [dataArray addObject:obj];
                }
            }
        }
    }
    @catch (NSException *exception) {
        NSLog(@"%@",[exception description]);
    }
    
    return dataArray;
}



/**
 *  @Author lpm, 15-08-03 16:08:19
 *
 *  @brief  获得子元素
 *
 */
-(UIView *)getItem:(NSMutableDictionary *)data frame:(CGRect)rect
{
    //交给子类重写
    return [UIView new];
}


/**
 *  @Author lpm, 15-08-03 16:08:19
 *
 *  @brief  滚动结束委托
 *
 *  @param scrollView
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger index = offsetX/self.frame.size.width;
    if (index>=0) {
        _pageControl.currentPage = index;
    }
}


/**
 *  @Author lpm, 15-08-03 16:08:19
 *
 *  @brief  子元素被点击
 *
 */
- (void)doAction:(UITapGestureRecognizer *)recognizer
{
    [self item:recognizer.view didSelected:recognizer.view.tag];
}



/**
 *  @Author lpm, 15-08-03 16:08:19
 *
 *  @brief  子元素被点击
 *
 */
-(void)item:(UIView *)view didSelected:(NSInteger)index
{
    //子类重写
}

@end
